/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.samples;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import javax.persistence.*;
import org.balisunrise.annotation.ColumnEmbedded;
import org.balisunrise.annotation.CriteriaEmbedded;
import org.balisunrise.annotation.FieldDefinition;
import org.balisunrise.annotation.FieldSelection;
import org.balisunrise.annotation.FilterCriteria;
import org.balisunrise.annotation.Grid;
import org.balisunrise.annotation.GridColumn;
import org.balisunrise.annotation.Panel;
import org.balisunrise.annotation.PanelField;
import org.balisunrise.annotation.VisualEntity;
import org.hibernate.annotations.Type;

/**
 *
 * @author Glauco
 */
@Embeddable
@VisualEntity(
    panels={@Panel(alias="def",title="Endereço")},
    grids={@Grid(alias="cid",title="Endereço")}
)
public class Endereco implements Serializable{

    // <editor-fold defaultstate="collapsed" desc="ChangeSupport">
    @Transient
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }
    public static final String PROP_LOGRADOURO = "logradouro";
    public static final String PROP_NUMERO = "numero";
    public static final String PROP_CEP = "cep";
    public static final String PROP_BAIRRO = "bairro";
    public static final String PROP_CIDADE = "cidade";
    public static final String PROP_COMPLEMENTO = "complemento";
    public static final String PROP_FONE = "fone";
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="annotations">
    @Column(length = 9, columnDefinition = "char(9)")
    @Type(type = "org.balisunrise.dao.hibernate.TrimmedString")
    @PanelField(definitions = {
        @FieldDefinition(alias = "def", label = "CEP")})
    // </editor-fold>
    private String cep;
    
    // <editor-fold defaultstate="collapsed" desc="annotations">
    @ManyToOne
    @PanelField(selection = {
        @FieldSelection(alias = "def", panel = "sel", search = "def",
        notFoundMessage = "Cidade não encontrada!",index=1)})
    // </editor-fold>
    private Cidade cidade;

    // <editor-fold defaultstate="collapsed" desc="annotations">
    @Column(length = 50, columnDefinition = "char(50)")
    @Type(type = "org.balisunrise.dao.hibernate.TrimmedString")
    @PanelField(definitions = {
        @FieldDefinition(alias = "def", label = "Bairro")})
    // </editor-fold>
    private String bairro;

    // <editor-fold defaultstate="collapsed" desc="annotations">
    @Column(length = 50, columnDefinition = "char(50)")
    @Type(type = "org.balisunrise.dao.hibernate.TrimmedString")
    @PanelField(definitions = {
        @FieldDefinition(alias = "def", label = "Logradouro")})
    // </editor-fold>
    private String logradouro;

    // <editor-fold defaultstate="collapsed" desc="annotations">
    @Column(length = 10, columnDefinition = "char(10)")
    @Type(type = "org.balisunrise.dao.hibernate.TrimmedString")
    @PanelField(definitions = {
        @FieldDefinition(alias = "def", label = "Nº")})
    // </editor-fold>
    private String numero;

    // <editor-fold defaultstate="collapsed" desc="annotations">
    @Column(length = 100)
    @PanelField(definitions = {
        @FieldDefinition(alias = "def", label = "Complemento")})
    // </editor-fold>
    private String complemento;

    // <editor-fold defaultstate="collapsed" desc="annotations">
    @Column(length = 14, columnDefinition = "char(14)")
    @Type(type = "org.balisunrise.dao.hibernate.TrimmedString")
    @PanelField(definitions = {
        @FieldDefinition(alias = "def", label = "Fone")})
    // </editor-fold>
    private String fone;

    // <editor-fold defaultstate="collapsed" desc="Sets & Gets">
    public String getFone() {
        return fone;
    }

    public void setFone(String fone) {
        String oldFone = this.fone;
        this.fone = fone;
        propertyChangeSupport.firePropertyChange(PROP_FONE, oldFone, fone);
    }

    public String getComplemento() {
        return complemento;
    }

    public void setComplemento(String complemento) {
        String oldComplemento = this.complemento;
        this.complemento = complemento;
        propertyChangeSupport.firePropertyChange(
                PROP_COMPLEMENTO, oldComplemento, complemento);
    }

    public Cidade getCidade() {
        return cidade;
    }

    public void setCidade(Cidade cidade) {

        if(cidade == null)
            throw new RuntimeException("Cidade deve ser informada!");

        Cidade oldCidade = this.cidade;
        this.cidade = cidade;
        propertyChangeSupport.firePropertyChange(
                PROP_CIDADE, oldCidade, cidade);
    }

    public String getBairro() {
        return bairro;
    }

    public void setBairro(String bairro) {

        if(bairro == null || bairro.length() == 0)
            throw new RuntimeException("Bairro deve ser informado!");

        String oldBairro = this.bairro;
        this.bairro = bairro;
        propertyChangeSupport.firePropertyChange(
                PROP_BAIRRO, oldBairro, bairro);
    }

    public String getNumero() {
        return numero;
    }

    public void setNumero(String numero) {

        if (numero == null || numero.length() == 0)
            throw new RuntimeException("Número deve ser informado!");

        String oldNumero = this.numero;
        this.numero = numero;
        propertyChangeSupport.firePropertyChange(
                PROP_NUMERO, oldNumero, numero);
    }

    public String getLogradouro() {
        return logradouro;
    }

    public void setLogradouro(String logradouro) {

        if (logradouro == null || logradouro.length() == 0)
            throw new RuntimeException("Logradouro deve ser informado!");

        String oldLogradouro = this.logradouro;
        this.logradouro = logradouro;
        propertyChangeSupport.firePropertyChange(
                PROP_LOGRADOURO, oldLogradouro, logradouro);
    }

    public String getCep() {
        return cep;
    }

    public void setCep(String cep) {
        String oldCep = this.cep;
        this.cep = cep;
        propertyChangeSupport.firePropertyChange(PROP_CEP, oldCep, cep);
    }
    // </editor-fold>
}
